package com.kcsj.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
 * @author 傅立贤
 * 25th Nov. 2020
 * 发送邮件的Utils工具类
 */
public class SendMailUitls {
    public static void sendMail(String to,String code) throws MessagingException {
        // 发件人电子邮箱
        String from = "471444261@qq.com";
        // 指定发送邮件的主机为 smtp.qq.com
        String host = "smtp.qq.com";
        // 获取系统属性
        Properties properties = System.getProperties();
        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);
        //开启认证，需要验证用户名密码
        properties.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(properties,new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication(from, "gqukhracoybfbjbj");
            }
        });
        //创建邮件对象
        Message message = new MimeMessage(session);
        //设置发件人，这里我创建了一个邮箱用于发送邮件
        message.setFrom(new InternetAddress(from));
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
        message.setSubject("装配式建筑物流管理系统");
        message.setContent("欢迎您注册成为我们的客户," +
                "<a href='http://localhost:8080/kcsj/active.ctl?code="
                +code+"'>点击链接激活</a>","text/html;charset=utf-8");
        //发送邮件
        Transport.send(message);
    }
}